Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with not being able to update connections on organizations #244

Merged
merged 1 commit into from
Jul 18, 2022

Conversation

sergiught
Copy link
Contributor

@sergiught sergiught commented Jul 16, 2022

Description

Fixes: #65

Checklist

Note: Checklist required to be completed before a PR is considered to be reviewable.

Auth0 Code of Conduct

Auth0 General Contribution Guidelines

Changes include test coverage?

  • Yes
  • Not needed

Does the description provide the correct amount of context?

  • Yes, the description provides enough context for the reviewer to understand what these changes accomplish

Have you updated the documentation?

  • Yes, I've updated the appropriate docs
  • Not needed

Is this code ready for production?

  • Yes, all code changes are intentional and no debugging calls are left over

@sergiught sergiught self-assigned this Jul 16, 2022
@sergiught sergiught force-pushed the patch/fix-issue65-with-org-update branch 2 times, most recently from de8e692 to 0941fd7 Compare July 16, 2022 07:38
},
},
},
Set: hash.StringKey("connection_id"),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't use just the connection_id for the set hashes as that will ignore anything that we update on the assign_membership_on_login field. This is mostly the core of the fix, but in order to make it work properly several other things were needed. Please follow comments.

@@ -64,7 +60,6 @@ func newOrganization() *schema.Resource {
"connections": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need to be a computed property, it's actually us within the config that decide the values of these and then it gets created on the API.

@@ -74,83 +69,32 @@ func newOrganization() *schema.Resource {
"assign_membership_on_login": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This default is very important to correctly figure out the differences on the hashed sets.

},
},
}
}

func createOrganization(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
organization := buildOrganization(d)
organization := expandOrganization(d)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renaming this so we're in line with the naming conventions "expand" and "flatten".

api := m.(*management.Management)
if err := api.Organization.Create(organization); err != nil {
return diag.FromErr(err)
}

d.SetId(organization.GetID())

d.Partial(true)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Part of what caused the bug with the diffs I believe. The Partial func is a legacy func used when we had the SetPartial func as well (deprecated). This is not needed at all as on every command we run a refresh (read) even if we stop mid way here on the follow up plan or apply we'll read again and correct the state.

toAdd, toRemove := Diff(d, "connections")

var err error
toRemove.Elem(func(data ResourceData) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After we updated the schema we were getting at times weird errors about the connection already being there (409s).

This was because if we change the bool flag on a connection we get the following diff:

 - connections {
    - assign_membership_on_login = false -> null
    - connection_id              = "con_E68qtlsIB68K6Yz6" -> null
  }
  + connections {
    + assign_membership_on_login = true
    + connection_id              = "con_E68qtlsIB68K6Yz6"
  }

So to correctly update the connection without adding complicated diffing logic, we delete the connection and then re-add it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: We opted instead to create an internal map in the updateConnections function in order to not delete and then add even if the diff shows that in terraform. Unfortunately we couldn't suppress the diff correctly.

return err
}

Set(d, "connections").Elem(func(data ResourceData) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was also important to remove the HasChange() from here, so that now we always loop through the final connections and send a PATCH with whatever the value is, as this operation is idempotent. This ultimately makes us correctly update the values.

@@ -137,20 +99,114 @@ resource auth0_organization acme {
}
`

const testAccOrganizationUpdateAgain = testAccOrganizationAux + `
const testAccOrganizationUpdateAgain = testAccOrganizationGiven2Connections + `
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding more thorough test scenarios and assertions.

@sergiught sergiught marked this pull request as ready for review July 16, 2022 07:53
@sergiught sergiught requested a review from a team as a code owner July 16, 2022 07:53
@sergiught sergiught force-pushed the patch/fix-issue65-with-org-update branch from 0941fd7 to 6f32c87 Compare July 16, 2022 09:38
resource.TestCheckResourceAttr("auth0_organization.acme", "branding.0.colors.primary", "#e3e2f0"),
resource.TestCheckResourceAttr("auth0_organization.acme", "branding.0.colors.page_background", "#e3e2ff"),
resource.TestCheckResourceAttr("auth0_organization.acme", "metadata.%", "0"),
resource.TestCheckResourceAttr("auth0_organization.acme", "connections.#", "1"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing I observed while working on the organization members is that for whatever reason, the bulk of this code had issues with completely clearing out members. It's possible that the connections has the same issue, so for that reason I think having the following test case would be useful:

resource.TestCheckResourceAttr("auth0_organization.acme", "connections.#", "0"),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added another test case where we remove all connections 👍🏻 and tests pass

@sergiught sergiught force-pushed the patch/fix-issue65-with-org-update branch from 6f32c87 to d030ba2 Compare July 18, 2022 14:44
@sergiught sergiught force-pushed the patch/fix-issue65-with-org-update branch from d030ba2 to bfb8cf3 Compare July 18, 2022 15:28
@sergiught sergiught merged commit bd683cd into main Jul 18, 2022
@sergiught sergiught deleted the patch/fix-issue65-with-org-update branch July 18, 2022 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Organization connection does not get updated
3 participants